﻿using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
using APIDemo.Ali;
using Main.Ali;

namespace Main
{
    public partial class AliLogin : Form
    {
        #region 属性声明
        AliClient _aliClient = null;
        public bool LockUserName
        {
            get { return this.tbUsername.Enabled; }
            set { this.tbUsername.Enabled = value; }
        }
        public AliClient AliClient
        {
            get
            {
                return _aliClient;
            }
            set
            {
                _aliClient = value;
                if (_aliClient.AliLoginUser != null && _aliClient.AliLoginUser.person_data != null)
                {
                    UserName = _aliClient.AliLoginUser.person_data.login_id;
                }
            }
        }
        public string UserName
        {
            get
            {
                return (this.tbUsername.Text + "").Trim();
            }
            set
            {
                this.tbUsername.Text = value;
            }
        }
        public string Password
        {
            get
            {
                return (this.tbPwd.Text + "").Trim();
            }
            set
            {
                this.tbPwd.Text = value;
            }
        }
        private string CheckCode
        {
            get
            {
                return (this.tbCheckCode.Text + "").Trim();
            }
        }
        #endregion
        
        public AliLogin()
        {
            InitializeComponent();
            //初始化_aliClient，相当于打开一个浏览器，并设置一个空的cookie
            _aliClient=new AliClient {Cookie = new CookieContainer()};
            RefreshCheckCode();//无需验证码登陆时这里替换为PrepareLogin(_aliClient);
        }
        /// <summary>
        /// 先把验证码加载出来，这里是偷懒
        /// 正确做法:
        /// 1.无验证码直接登录->成功
        /// 2.失败->刷新出验证码->登陆->成功
        /// </summary>
        void RefreshCheckCode()
        {
            Image img = AliPassporter.DoCheckCode(_aliClient);
            checkCodeImage.Image = img;
        }
        private async void btnLogin_Click(object sender, EventArgs e)
        {
            //登陆系统，异步方法防止页面假死
            var data=await AliPassporter.DoLoginAsync(_aliClient, UserName, Password, CheckCode);
            if (data)
            {
                //登陆成功,登陆后其他数据就随便抓了,这里要注意，需要手机验证码的，要在网页中先把手机验证码输入了
                //我的测试账号默认写在里面，别乱玩就行了
                //进入管理个人信息页面
                var html = _aliClient.Get("http://accounts.alibaba.com/user/organization/manage_person_profile.htm");
                MessageBox.Show("登陆成功，此处可以进行其他操作了!");
                //_aliClient.Post("");
            }
            else
            {
                MessageBox.Show("登陆失败!");
            }
        }
        private void btnChangeImg_Click(object sender, EventArgs e)
        {
            RefreshCheckCode();
        }
    }
}
